/**
 * 本扩展是提供artDialog.confirm功能，支持的ajax分别以get、post两种方式提交到后台
 * 成功后刷新指定url
 */
//参数和默认值
var conf = {
    backUrl: "",              //成功后要跳转的url
    submitUrl:"",             //要提交到的url
    ajaxDataType : 'json',    //返回的数据格式
    icon: "warning",          //artDialog提示图标
    content: "ui_c_confirm_tip",
    cancelVal : '关闭',
    okVal : '确定',
    title:'提示：2秒后自动关闭',
    btnBack : '返回',
    tip: '提示',
    errorTip : '失败',
    processTip: '<table><tr><td><div class="aui_loading" style="width:20px;"><span>loading..</span></div></td><td>处理中，请稍候.....</td></table>',
    showProcessTip : true, //默认不打开确定后正在处理提示
    data:'',//&分隔的key-value格式的待提交数据,如test=aaa&name=bbbb
    data_form_id:'',//如果不提交data,则需要提供表单ID. 
    exit_time:2,//自动关闭时间 
    finishCall:''//回调函数
};

/**
 * get方式的确认
 */
function artGetConfirm(option){
    var title = option.title ? option.title : conf.title;
    var backurl = option.backUrl ? option.backUrl : conf.backUrl;
    var exit_time = option.exit_time ? option.exit_time : conf.exit_time;
    var openProcessTip = option.showProcessTip ? option.showProcessTip : conf.showProcessTip;
    var processContent = option.processTip ? option.processTip : conf.processTip;
    var dialog = $.dialog({
        content: option.content ? option.content : conf.content,
        okVal:conf.okVal,
        ok: function () {
            if(openProcessTip == true){
                var processDialog = $.dialog({
                    content: processContent,
                    lock: true,
                    id : 'EF89YU'
                });
            }
            jQuery.ajax({
                url: option.submitUrl ? option.submitUrl : conf.submitUrl,
                dataType: conf.ajaxDataType,
                data: option.data ? option.data : conf.data ,
                success: function (data) {
                    if(data.status ==1){
                        dialog.close();
                        get_confrim_result(data.info,'succeed',title,backurl,exit_time);
                        return false;
                    }else{
                        dialog.close();
                        get_confrim_result(data.info,'error',title,backurl);
                        return false;
                    }
                },
                error: function(){                     
                    error_dialog(conf.errorTip);     
                },
                complete : function(){
                    if(processDialog != undefined){
                        processDialog.close();
                    }
                },
                cache: false
            });
            return false;
        },        
        cancelVal: conf.cancelVal,
        cancel: true,
        id: 'EF893L',
        lock:true,
        icon:option.icon ? option.icon : conf.icon
    });
}
/**
 * POST方式的确认
 */
function artPostConfirm(option){
    var title = option.title ? option.title : conf.title;
    var backurl = option.backUrl ? option.backUrl : conf.backUrl;
    var exit_time = option.exit_time ? option.exit_time : conf.exit_time;
    var openProcessTip = option.showProcessTip ? option.showProcessTip : conf.showProcessTip;
    var processContent = option.processTip ? option.processTip : conf.processTip;
    var finishCall = option.finishCall ? option.finishCall : conf.finishCall;
    if((option.data == undefined )&&(option.data_form_id == undefined)){       
        error_dialog('no data or form_id found! can not post');     
        return false;
    } 
    var dialog = $.dialog({
        content: option.content ? option.content : conf.content,        
        ok: function () {
            if(openProcessTip == true){
                var processDialog = $.dialog({
                    content: processContent,
                    lock: true,
                    id : 'EF89YU'
                });
            }
            jQuery.ajax({
                type: "post",
                url: option.submitUrl ? option.submitUrl : conf.submitUrl,
                dataType: conf.ajaxDataType,
                data:(option.data != undefined)?option.data:$('#'+option.data_form_id).serialize(),
                success: function (data) {
                    if(data.status ==1){
                        dialog.close();
                        get_confrim_result(data.info,'succeed',title,backurl,exit_time,finishCall);
                        return false;
                    }else{
                        dialog.close();
                        get_confrim_result(data.info,'error',title,backurl);
                        return false;
                    }
                },
                error: function(){
                    error_dialog(conf.errorTip);                   
                },
                complete : function(){
                    if(processDialog != undefined){
                        processDialog.close();
                    }
                },
                cache: false
            });
            return false;
        },
        okVal:conf.okVal,
        cancelVal: conf.cancelVal,
        cancel: true,
        id: 'EF893L',
        lock:true,
        icon:option.icon ? option.icon : conf.icon
    });
}           
/**
 * get 的 ajax 返回结果
 */
function get_confrim_result(info,icon,title,backurl,exit_time,finishCall){
    //操作成功
    if(icon == 'succeed'){
        $.dialog({
            title: title,
            content : info,
            ok : function(){
                if(finishCall == undefined){
                    window.location = backurl;
                }else{
                    if(finishCall!=''){
                        finishCall();
                    }else{
                        window.location = backurl;
                    }
                }
            },
            cancel :function(){
                if(finishCall == undefined){
                    window.location = backurl;
                }else{
                    if(finishCall!=''){
                        finishCall();
                    }else{
                        window.location = backurl;
                    }
                }
            },
            cancelVal: conf.btnBack,
            id:'DEISF',
            lock:true,
            icon:icon,
            time:exit_time
        });
    }
    //操作失败
    if(icon == 'error'){
        $.dialog({
            title: conf.tip,
            content : info,
            ok : function(){
                this.close();
            },
            id:'DEISF',
            lock:true,
            icon:icon
        });
    }
}
/**
 *显示错误对话框
 */
function error_dialog(content,go_url,close_time){
    if(go_url != undefined){
        $.dialog({
            icon: 'error',
            id:'EYSOI#JE',
            title:'错误',
            time:close_time== undefined?5:close_time,
            content:content,
            close: function () {
                if(go_url == 'reload'){
                    window.location.reload();  
                }else if(go_url != ''){
                    window.location.href = go_url;
                } 
            }
        }); 
    }else{
        $.dialog({
            icon: 'error',
            id:'EYSOI#JE',
            title:'错误',
            time:close_time== undefined?5:close_time,
            content:content
        }); 
    }
    
}
/**
 *显示成功对话框
 */
function success_dialog(content,next_url,close_time){    
    if(next_url != undefined){
        $.dialog({
            icon: 'succeed',
            id:'EYSOI#JSU',
            time: close_time== undefined?1:close_time,
            title:'当前操作成功了！',
            content:content,
            close: function () {
                if(next_url == 'reload'){
                    window.location.reload();  
                }else if(next_url != ''){
                    window.location.href = next_url;
                } 
            }
        });
    }else{
        $.dialog({
            icon: 'succeed',
            id:'EYSOI#JS',
            title:'确认',
            time: close_time== undefined?1:close_time,
            content:content
        }); 
    }
    
}
/**
 *显示警告对话框
 */
function warning_dialog(content,go_url,close_time){
    if(go_url != undefined){
        $.dialog({
            icon: 'warning',
            id:'EYSOI#JW',
            title:'警告',
            time:close_time== undefined?5:close_time,
            content:content,
            close: function () {
                if(go_url == 'reload'){
                    window.location.reload();  
                }else if(go_url != ''){
                    window.location.href = go_url;
                } 
            }
        }); 
    }else{
        $.dialog({
            icon: 'warning',
            id:'EYSOI#JW',
            title:'警告',
            time: close_time== undefined?5:close_time,
            content:content
        });
    }
    
}
/**
 *显示信息对话框
 */
function info_dialog(content,go_url,close_time,title){
    if(go_url != undefined){
        $.dialog({
            icon: 'face-sad',
            id:'EYSOI#JI',
            title:title == undefined?'提示信息':title,
            time:close_time == undefined?5:close_time,
            content:content,
            close: function () {
                if(go_url == 'reload'){
                    window.location.reload();  
                }else if(go_url != ''){
                    window.location.href = go_url;
                } 
            }
        }); 
    }else{
        $.dialog({
            icon: 'face-sad',
            id:'EYSOI#JI',
            title:'提示信息',
            time: close_time== undefined?5:close_time,
            content:content
        });
    }    
}
/**
 * artDialog的confirm中get提交方式再封装
 * url 要提交的url
 * 提示的第一行内容,可以填写受此操作影响的数据关键内容.
 * tip:真正的提示信息.manage_host_confirm
 */
function confirm_get(url,pre_tip,tip,backurl){
    artGetConfirm({
        submitUrl : url,//ui_z_record_stop_confirm
        backUrl:backurl,
        content:pre_tip+'<br>'+tip               
    });
}
/**
 *直接ajax get调用,
 *数据类型为json
 *假定返回的数据中的data一定是一个url
 */
function direct_get(url){
    $.ajax({
        type: "get",
        url: url,
        dataType:"json",
        cache:false,			
        success: function(ret){                        
            if(ret.status == 1){
                if(ret.data != ''){//假定返回的数据一定是URL.
                    success_dialog(ret.info,ret.data);
                }else{
                    success_dialog(ret.info); 
                }              
            }else{
                if(ret.data != ''){//假定返回的数据一定是URL.
                    error_dialog(ret.info,ret.data,5);
                }else{
                    error_dialog(ret.info); 
                }
            }   
        },
        error: function(){
            error_dialog(conf.errorTip);
        }		
    });
}
/**
 *直接ajax post调用,为简单post封装
 *数据类型为json option
 *增加了一个内部参数show_success_info,表达是否显示成功的信息
 */
function direct_post(option){
    //url,post_data,next_url,show_success_info
    var show_success_info = option.show_success_info?option.show_success_info:false;
    var backUrl = option.backUrl ? option.backUrl : conf.backUrl;
    if((option.data == undefined )&&(option.data_form_id == undefined)){
        $.dialog({
            icon: 'error',
            title:'错误',
            content:'no data or form_id found! can not post'
        });
        return false;
    }
    $.ajax({
        type:'post',
        dataType:'json',
        data:(option.data != undefined)?option.data:$('#'+option.data_form_id).serialize(),
        url:option.submitUrl ? option.submitUrl : conf.submitUrl,
        success:function (data){            
            if(data.status == 1){
                if(show_success_info){
                    success_dialog(data.info,backUrl);
                }else{
                    window.location.href = backUrl;
                }
            }else{
                warning_dialog(data.info);
            }
        },
        error:function(){
            error_dialog(conf.errorTip);         
        }
    });
}
